package com.jw.qme.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.jw.qme.domain.dto.FriendDTO;
import com.jw.qme.domain.dto.UserDTO;
import com.jw.qme.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: com.jw.qme.controller
 * Datetime: 2025/4/5 下午6:45
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @PostMapping("/user/register")
    public SaResult userRegister(@RequestBody UserDTO userDTO){
        return SaResult.data(userService.userRegister(userDTO));
    }

    /**
     * 用户登入
     * @param userDTO
     * @return
     */
    @PostMapping("/user/login")
    public SaResult userLogin(@RequestBody UserDTO userDTO){
        return SaResult.data(userService.userLogin(userDTO));
    }

    /**
     * 用户退出
     * @return
     */
    @GetMapping("/user/logout")
    public SaResult userLogout(){
        StpUtil.checkLogin();
        StpUtil.logout();
        return SaResult.ok("成功退出");
    }

    /**
     * 接收前端生成的对称密钥
     * @param symmetricKey
     * @return
     * @throws Exception
     */
    @PostMapping("/user/public")
    public SaResult sendSymmetricKey(@RequestBody String symmetricKey) throws Exception {
        return SaResult.data(userService.sendSymmetricKey(symmetricKey));
    }

    /**
     * 获取公钥
     * @return
     */
    @GetMapping("/user/public")
    public SaResult getPublicKey(){
        return SaResult.data(userService.getPublicKey());
    }

    /**
     * 查询用户
     */
    @GetMapping("/user/find/{searchkey}")
    public SaResult findUser(@PathVariable String searchkey){
        return SaResult.data(userService.findUser(searchkey));
    }

    /**
     * 处理好友申请
     * @param friendDTO
     * @return
     */
    @PostMapping("/user/friend/deal")
    public SaResult acceptUser(@RequestBody FriendDTO friendDTO){
        return SaResult.data(userService.friend(friendDTO));
    }

}
